package com.javasm.adminapi.module.system.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.javasm.adminapi.common.R;
import com.javasm.adminapi.common.exception.ExceptionEnum;
import com.javasm.adminapi.common.exception.JavasmException;
import com.javasm.adminapi.module.system.dto.IdsDTO;
import com.javasm.adminapi.module.system.dto.PasswordUpdateDTO;
import com.javasm.adminapi.module.system.dto.UserQueryDTO;
import com.javasm.adminapi.module.system.entity.User;
import com.javasm.adminapi.module.system.service.UserApiService;
import com.javasm.adminapi.module.system.service.UserService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户表(User)表控制层
 *
 * @author makejava
 * @since 2025-10-15 14:45:54
 */
@RestController
@RequestMapping("/admin/user")
public class UserController{
    @Resource
    private UserApiService userApiService;

    //修改当前用户信息
    @PostMapping("/upload/info")
    public R uploadInfo(@RequestBody User user){
        System.out.println( user);
        User user1 = userApiService.updateByUser(user);
        return R.ok(user1);
    }

    //修改密码
    @PostMapping("/updatePwd")
    public R updatePwd(@RequestBody PasswordUpdateDTO passwordUpdateDTO){
        userApiService.updatePwd(passwordUpdateDTO.getOldPassword(),passwordUpdateDTO.getNewPassword());
        return R.ok();
    }

    @Resource
    private UserService userService;
    //查询
    @GetMapping("/page")
    public R page(UserQueryDTO userQueryDTO){
        System.out.println(userQueryDTO);
        PageInfo<User> list = userService.pageList(userQueryDTO);
        return R.ok(list);
    }

    //保存加修改
    @PostMapping("/save/update")
    public R saveUpdate(@RequestBody User user){
        userService.saveUpdate( user);
        return R.ok();
    }
    //删除用户
    @DeleteMapping("/del/{ids}")
    public R delete(@PathVariable("ids") String ids){
        //删除
        if(StringUtils.isEmpty(ids)){
            throw new JavasmException(ExceptionEnum.Parameter_Null);
        }
        List<Long> collect =
                Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        userService.delByIds(collect);
        return R.ok();
    }

    //批量重置密码
    @PostMapping("/resetPassword")
    public R resetPassword(@RequestBody IdsDTO ids){
        List<Long> collect = Arrays.stream(ids.getIds().split(","))
                .map(Long::parseLong).collect(Collectors.toList());
        userService.resetPassword( collect);
        return R.ok();
    }

    //修改状态
    @PostMapping("/updateStatus")
    public R updateStatus(@RequestBody User user){
        userService.updateStatic(user);
        return R.ok();
    }


}

